﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using KPIS.GERP.GBMS.APP_UI.Common;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class ProjectJobAssigneeList : System.Web.UI.Page
    {
        IProjectJobAssigneeListBLL ProjectJobAssignee = new ProjectJobAssigneeListBLL();
        Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string strProjJobAssignSeq = Request.QueryString["seq"];
                string strJobSite = Session["ProjectJobSiteSeq"].ToString();
                string strProjJobAssigneeSeq = lblProjJobAssignee.Text;
                string strPosName = lblPosName.Text;
                lblProjJobAssign.Text = strProjJobAssignSeq;
                lblJobSite.Text = strJobSite;
                lblPosName.Text = strPosName;

                ddlEmployeeList.DataSource = ProjectJobAssignee.LoadEmployeeList(null, null, strJobSite, null);
                ddlEmployeeList.DataBind();

                ddlEmployeeList.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกผู้รับผิดชอบ", ""));

                ChangeMode(Main.EditMode.View);
            }
        }

        private void LoadGtgProjJobAssignee()
        {
            string strProjJobAssignSeq = lblProjJobAssign.Text;
            dtEmployeeList.DataSource = ProjectJobAssignee.Search(null, strProjJobAssignSeq, null, null);

            dtEmployeeList.DataBind();
        }
        
        private void ClearData()
        {
            ddlEmployeeList.SelectedValue = "";
            txtRemark.Text = "";
        }

        private void ChangeMode(Main.EditMode mode)
        {
            switch (mode)
            {
                case Main.EditMode.AddNew:
                    pnlInfo.Visible = true;
                    dtEmployeeList.Visible = false;
                    lblModeName.Text = "เพิ่มรายการใหม่";

                    AddNew.Visible = false;
                    btnOk.Visible = true;
                    btnUpdate.Visible = false;
                    break;

                case Main.EditMode.Edit:
                    pnlInfo.Visible = true;
                    dtEmployeeList.Visible = false;
                    lblModeName.Text = "แก้ไขรายการ";

                    AddNew.Visible = false;
                    btnOk.Visible = false;
                    btnUpdate.Visible = true;
                    break;

                case Main.EditMode.View:
                    pnlInfo.Visible = false;
                    dtEmployeeList.Visible = true;

                    AddNew.Visible = true;
                    btnOk.Visible = false;
                    btnUpdate.Visible = false;
                    break;
            }
        }

        private bool CheckNullValue()
        {
            bool bOk = true;
            string strEmpSeq = ddlEmployeeList.SelectedValue;
            string strRemark = txtRemark.Text;

            if (strEmpSeq.Length == 0)
            {
                main.MessageAlert(Page, "เลือกบุคลากร");
                bOk = false;
            }
            else if (strRemark.Trim().Length == 0)
            {
                main.MessageAlert(Page, "ระบุหมายเหตุ");
                bOk = false;
            }
            return bOk;
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            ClearData();
            ChangeMode(Main.EditMode.View);
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string strProjJobAssignSeq = lblProjJobAssign.Text;
            string strEmpSeq = ddlEmployeeList.SelectedValue;
            string strRemark = txtRemark.Text;
            string strPosName = lblPosName.Text; 

            if (!CheckNullValue())
            {
                return;
            }

            bool isDuplicated = ProjectJobAssignee.CheckDuplicateCode(strEmpSeq, strProjJobAssignSeq);

            if (isDuplicated == false)
            {
                    ProjectJobAssignee.Insert(strProjJobAssignSeq, strEmpSeq, strRemark);

                    ClearData();
                    LoadGtgProjJobAssignee();
                    ChangeMode(Main.EditMode.View);

                    main.MessageAlert(Page, "บันทึกข้อมูลเรียบร้อยแล้ว");
            }
            else
            {
                main.MessageAlert(Page, "รายชื่อบุคลากรนี้มีอยู่แล้วในงาน");
            }

        }

        protected void dtEmployeeList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            string strProjJobAssignSeq = Request.QueryString["seq"];
            dtEmployeeList.DataSource = ProjectJobAssignee.Search(null, strProjJobAssignSeq, null, null);
        }        
        
        protected void AddNew_Click(object sender, EventArgs e)
        {
            ClearData();
            lblModeName.Text = "เพิ่มรายการใหม่";
            ChangeMode(Main.EditMode.AddNew);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strProjJobAssigneeSeq = lblProjJobAssignee.Text;
            string strEmpSeq = ddlEmployeeList.SelectedValue;
            string strRemark = txtRemark.Text;
            string strProjJobAssignSeq = lblProjJobAssign.Text;
            
            if (!CheckNullValue())
            {
                return;
            }

            if (ProjectJobAssignee.CheckDuplicateCode(strEmpSeq,strProjJobAssigneeSeq))
            {
                main.MessageAlert(Page, "รายชื่อบุคลากรนี้มีอยู่แล้วในงาน");
                return;
            }
                ProjectJobAssignee.Update(strProjJobAssigneeSeq, strEmpSeq, strRemark);

                LoadGtgProjJobAssignee();
                ClearData();
                ChangeMode(Main.EditMode.View);
                main.MessageAlert(Page, "แก้ไขข้อมูลเรียบร้อยแล้ว");
                
           }

        protected void dgtEmployeeList_Itemcommand(object sender, GridCommandEventArgs e)
        {
            string strCommandName = e.CommandName;

            if (strCommandName == "GridEdit")
            {
                ClearData();
                ChangeMode(Main.EditMode.Edit);

                GridDataItem item = (GridDataItem)e.Item;

                lblModeName.Text = "แก้ไขรายการ";
                lblProjJobAssignee.Text = main.WebString(item["bgm_proj_job_assignee_seq"].Text);
                ddlEmployeeList.SelectedValue = main.WebString(item["emp_seq"].Text);
                txtRemark.Text = main.WebString(item["job_remark"].Text);

            }
            else if (strCommandName == "GridDelete")
            {
                GridDataItem item = (GridDataItem)e.Item;

                string seq = main.WebString(item["bgm_proj_job_assignee_seq"].Text);
                ProjectJobAssignee.Delete(seq);

                LoadGtgProjJobAssignee();
            }
            else if (strCommandName == "ViewDetail")
            {
                GridDataItem item = (GridDataItem)e.Item;

                string seq = main.WebString(item["bgm_proj_job_assign_seq"].Text);

           }
        }
    }
}